//
//  RMWriteShortNoteViewController.m
//  RMNote
//
//  Created by 晓风残月 on 16/4/21.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMWriteShortNoteViewController.h"
#import "RMDateBaseManager.h"
#import "RMBaseModel.h"
@interface RMWriteShortNoteViewController ()
@property (nonatomic , assign) BOOL isHaveModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomDistanceAboutTextView;
@end

@implementation RMWriteShortNoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"写便签";
    self.navigationItem.title = @"写便签";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:(UIBarButtonItemStyleDone) target:self action:@selector(finishAction:)];
    if (!self.model) {
        self.model = [[RMBaseModel alloc]init];
        self.model.createDate = [NSDate date];
        self.isHaveModel = 1;
    }
    self.timeLabel.text = [NSString stringWithFormat:@"%@-%@-%@  %@:%@:%@",_model.createDate.year,_model.createDate.month,_model.createDate.day,_model.createDate.hour,_model.createDate.minute,_model.createDate.second];;
   self.contentTextView.text = _model.content;
    
    
    //mark!!!:给键盘添加通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardNotification:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardNotification:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)finishAction:(id)sender{
    if (self.isHaveModel == 1) {
        self.model.content = self.contentTextView.text;
        [[RMDateBaseManager defaultManager]createTable:kRMShortNoteTable];
        [[RMDateBaseManager defaultManager]insertIntoTable:kRMShortNoteTable createDate:self.model.createDate Model:self.model];
    }
    self.model.content = self.contentTextView.text;
    [[RMDateBaseManager defaultManager]updateNoteFromTableName:kRMShortNoteTable withModel:self.model byCreateDate:self.model.createDate];
 
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidAppear:(BOOL)animated{
//     self.timeLabel.text = [NSString stringWithFormat:@"%@",_model.createDate];
    self.contentTextView.text = _model.content;
}
- (void)onKeyboardNotification:(NSNotification *)notification {
    
    if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
        [self.contentTextView becomeFirstResponder];
        CGRect keyboardFrame = ((NSValue *) notification.userInfo[UIKeyboardFrameEndUserInfoKey]).CGRectValue;
//        self.keyboardFrame = keyboardFrame;
        _bottomDistanceAboutTextView.constant = keyboardFrame.size.height + 30;
        NSLog(@"--------%f",_bottomDistanceAboutTextView.constant);
        
    } else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {
        [self.contentTextView resignFirstResponder];
        _bottomDistanceAboutTextView.constant = 30;
        if (self.navigationController.isNavigationBarHidden == YES ) {
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
        
    }
 
        [self.view layoutIfNeeded];
    
    
}



@end
